<template>
	<view class="pageAll">
		<view class="pageTop">
			<view class="page-top-view">
				<image :src="labelRouge"></image>
			</view>
			<!-- 标语说不要了，先注释掉 -->
			<!-- <view class="page-top-view">
				<text>{{slogen}}</text>
			</view> -->
			<!-- 标语说不要了，先注释掉 -->
		</view>
		<view>
			<view class="formView">
				<view class="formViewInput">
					<input type="number" v-model="form.tel" placeholder="请输入手机号码" placeholder-style="color:#b57979" />
				</view>
				<view class="formViewInput">
					<input type="text" password v-model="form.password" placeholder="请输入登录密码" placeholder-style="color:#b57979" />
				</view>
			</view>
			<view class="formMiddle">
				<navigator class="formMiddleLeft" url="../registered/registered" hover-class="formMiddleHover">
					注册
				</navigator>
				<navigator class="formMiddleRight" :url="'../resetPwd/resetPwd?tel=' + form.tel" hover-class="formMiddleHover">
					忘记密码
				</navigator>
			</view>
			<view class="submitView">
				<button @click="submit" hover-class="subActive">
					登录
				</button>
			</view>
		</view>
		<logo class="Bottomlogo"></logo>
	</view>
</template>

<script>
	import logo from '../../../components/logo/bottomLogo.vue'
	import { HBValidate } from '../../../utils/tools.js'
	export default {
		data() {
			return {
				labelRouge: require('../../../static/index/labelRouge.png'),
				slogen: '标语的位置',
				form: {
					tel: '',
					password: '',
				}, // 登录的参数
				userType: null, // 用户类型
				isWorking: false, // 防止用户多次点击 
			}
		},
		methods: {
			// 登录
			submit() {
				const validate = new HBValidate()
				if(!validate.isPhone(this.form.tel)) {
					uni.showToast({
						title: '手机号码格式错误',
						duration: 2000,
						icon: 'none',
					})
				} else if(!validate.isPwd(this.form.password)) {
					uni.showToast({
						title: '登录密码格式错误',
						duration: 2000,
						icon: 'none',
					})
				} else {
					if(this.isWorking) {
						return false
					} else {
						this.isWorking = true
					}
					this.$api.supplierLogin({
							username: this.form.tel,
							password: this.form.password,
						})
						.then(res => {
							if (res.status) {
								// 保存至缓存 联系电话
								uni.setStorage({
									key: 'userTel',
									data: this.form.tel,
									success: function() {}
								});
								// 保存至缓存 密码
								uni.setStorage({
									key: 'userPwd',
									data: this.form.password,
									success: function() {}
								});
								
								let tempType = ''
								if (res.data.type === 'user') {
									tempType = 1
								} else {
									tempType = 2
								}
								// *******先注释
								if (res.data.status === 'unreviewed') { // 用户状态 未审核
									if (res.data.type === 'user') {
										uni.navigateTo({
											url: '../ImprovePerInfo/ImprovePerInfo'
										})
									} else {
										uni.navigateTo({
											url: '../ImproveCorInfo/ImproveCorInfo'
										})
									}
								} else if (res.data.status === 'checked') { // 已通过
									uni.switchTab({
										url: '../../index/index/index'
									})
								} else if (res.data.status === 'audit') { // 审核中
									uni.navigateTo({
										url: '../underReview/underReview'
									})
								} else if (res.data.status === 'pass') { // 审核已通过
									uni.navigateTo({
										url: '../alreadyPass/alreadyPass'
									})
								} else if (res.data.status === 'fail') { // 审核失败
									uni.navigateTo({
										url: '../noPass/noPass?userType=' + tempType
									})
								} else if (res.data.status === 'message_finish') { //已经填写好信息 没提交审核
									uni.navigateTo({
										url: '../cooperationSigning/cooperationSigning?userType=' + tempType
									})
								}
								this.$store.commit('setToken', res.token)
								this.$store.commit('setUserInfo', res.data)
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1500,
								})
							}
							this.isWorking = false
						})
						.catch(err => {
							this.isWorking = false
						})
				}
			},
		},
		onLoad() {
			uni.getStorage({
				key: 'userTel',
				success: res => {
					this.form.tel = res.data;
				}
			});
			uni.getStorage({
				key: 'userPwd',
				success: res => {
					this.form.password = res.data;
				}
			});
			
		},
		onShow() {
			if(uni.getStorageSync('nstorage_state')){
				uni.reLaunch({
					url:'../../index/index/index'
				})
			}
			
		},
		onReady() {

		},
		components: {
			logo, // 底部logo
		},
		onShareAppMessage: (options) => {
			// 设置菜单中的转发按钮触发转发事件时的转发内容
			var shareObj = {
				title: "红标HONBIAO", // 默认是小程序的名称(可以写slogan等)
				path: '/pages/indexPage/authorization/authorization', // 默认是当前页面，必须是以‘/’开头的完整路径
				imageUrl: '/static/index/labelRouge.png', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
				success: (res) => {
					// 转发成功之后的回调
					if (res.errMsg == 'shareAppMessage:ok') {}
				},
				fail: () => {
					// 转发失败之后的回调
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						// 用户取消转发
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				}
			};
			return shareObj
		},
	}
</script>

<style scoped>
	.pageAll {
		box-sizing: border-box;
		background-color: #fdf5f3;
		width: 100%;
		height: 100%;
		padding: 0 17upx 0 17upx;
		display: inline-block;
	}

	.pageTop {
		padding-top: 20upx;
		width: 100%;
		text-align: center;
		margin-bottom: 55upx;
	}

	.pageTop .page-top-view {
		margin-top: 20upx;
	}

	.pageTop image {
		width: 368upx;
		margin: 0 auto;
		height: 249.33upx;
	}

	.pageTop text {
		font-size: 54upx;
		color: #f72d09;
	}

	.formView {
		margin-top: 13.33upx;
	}

	.formViewInput {
		width: 100%;
		margin-bottom: 13.33upx;
	}

	.formViewInput input {
		border-radius: 8upx;
		background-color: #FFF;
		box-shadow: 0 0 10upx rgba(57, 11, 11, 0.15);
		height: 100upx;
		color: #b57979;
		font-size: 32upx;
		padding: 0 20upx;
	}

	.formMiddle {
		font-size: 32upx;
		color: #f7533e;
		overflow: hidden;
	}

	.formMiddle .formMiddleLeft {
		display: inline-block;
		float: left;
	}

	.formMiddle .formMiddleRight {
		display: inline-block;
		float: right;
	}

	.formMiddleHover {
		color: #FFC8C7;
	}

	.submitView {
		margin-top: 340upx;
	}
	.submitView button:after{
		border:none;
	}
	.submitView button {
		font-size:36upx;
		background-color: #f7533e;
		color: #FFF;
		letter-spacing: 24upx;
		height:100upx;
		line-height: 100upx;
		text-indent: 24upx;
		font-weight: 600;
	}

	.subActive {
		background-color: #FFC8C7 !important;
	}

	.Bottomlogo {
		position: fixed;
		bottom: 0;
		width: 100%;
	}
</style>
